package com.my.main.service.userAndRight.serviceImpl;

import com.my.main.base.exception.MyException;
import com.my.main.base.mapper.BaseMapper;
import com.my.main.base.service.Impl.BaseServiceImpl;
import com.my.main.common.utils.IdWorker;
import com.my.main.entity.userAndRight.SysRightsRoleUserRelation;
import com.my.main.entity.userAndRight.SysUser;
import com.my.main.mapper.userAndRight.SysRightsRoleUserRelationMapper;
import com.my.main.mapper.userAndRight.SysUserMapper;
import com.my.main.service.userAndRight.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    public IdWorker idWorker;
    @Autowired
    private SysRightsRoleUserRelationMapper sysRightsRoleUserRelationMapper;
    @Resource(name= "sysUserMapper")
    public void setBaseMapper(BaseMapper<SysUser> sysUserMapper) {
        super.setBaseMapper(sysUserMapper);
    }


    @Override
    public SysUser findByMobile(String phone) {
        return sysUserMapper.findByMobile(phone);
    }

    @Override
    public SysUser findByUserName(String userName) {
        return sysUserMapper.findByUserName(userName);
    }


    @Override
    public List<SysRightsRoleUserRelation> queryUserRole(String userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("limit", 0);
        return sysRightsRoleUserRelationMapper.queryListByMap(map);
    }

    @Override
    public void isSvaeUserRole(String userId, List<String> roleIdList, String handleUserId, String ip) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        sysRightsRoleUserRelationMapper.isDeleteByObject(map);
        for(String roleId : roleIdList) {
            SysRightsRoleUserRelation userRole = new SysRightsRoleUserRelation();
            userRole.setRelationId(idWorker.nextId() + "");
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRole.setAddIp(ip);
            userRole.setAddUserId(handleUserId);
            userRole.setLastEditIp(ip);
            userRole.setLastEditBy(handleUserId);
            userRole.setIsDel(false);
            sysRightsRoleUserRelationMapper.isSave(userRole);
        }
    }

    @Override
    public void isSave(SysUser sysUser, SysRightsRoleUserRelation sysRightsRoleUserRelation) {
        try {
            sysUserMapper.isSave(sysUser);
            sysRightsRoleUserRelationMapper.isSave(sysRightsRoleUserRelation);
        } catch (Exception e) {
            logger.error(e);
            throw new MyException(e);
        }

    }

    @Override
    public List<Map<String, Object>> queryUserByRoleId(String roleId) {
        return sysUserMapper.queryUserByRoleId(roleId);
    }

}
